<?php

namespace app\controllers;

use app\models\LoginForm;
use app\components\Check;
use app\models\Personinfo;
use app\models\DorminfoModel;
use app\models\SchoolinfoModel;
use app\models\PasswordAlertForm;
use yii\web\NotFoundHttpException;

class FrontController extends \yii\web\Controller {
	public $layout = 'front';
	public function actionIndex() {
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			$model = Personinfo::find ()->where ( [ 
					'stu_id' => $id 
			] )->one ();
			return $this->render ( 'index', [ 
					'model' => $model 
			] );
		} else
			return $this->redirect ( 'login' );
	}
	public function actionLogin() {
		$this->layout = 'login';
		$model = new LoginForm ();
		if ($model->load ( \Yii::$app->request->post () ) && $model->validate ()) {
			if (! $model->login ()) {
				\Yii::$app->session->setFlash ( 'warning', $model->message );
			}
			// 登录成功
			return $this->redirect ( 'index' );
		}
		return $this->render ( 'login', [ 
				'model' => $model 
		] );
	}
	
	/**
	 * 学生界面：修改个人信息
	 *
	 * @return \yii\web\Response|string
	 */
	public function actionUpdate() {
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			$model = Personinfo::find ()->where ( [ 
					'stu_id' => $id 
			] )->one ();
			
			if ($model->load ( \Yii::$app->request->post () ) && $model->save ()) {
				return $this->redirect ( 'index' );
			} else {
				return $this->render ( 'updateForm', [ 
						'model' => $model 
				] );
			}
		} else
			return $this->redirect ( 'login' );
	}
	public function actionLogout() {
		if (Check::check ()) {
			if (\Yii::$app->request->isPost) {
				$session = \Yii::$app->session;
				$session->remove ( 'stu' ); // 移除学生session
				return $this->redirect ( 'login' );
			} else {
				exit ( '哈哈哈，失败了！' );
			}
		} else
			return $this->redirect ( 'login' );
	}
	public function actionDorm() { // 查看住宿信息
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			
			return $this->render ( 'Dorm', [ 
					'id' => $id 
			] );
		} else
			return $this->redirect ( 'login' );
	}
// 	public function actionNewdorm() // 新建住宿信息
// {
// 		if (Check::check ()) {
// 			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
// 			$model = new DorminfoModel ();
			
// 			if ($model->load ( \Yii::$app->request->post () ) && $model->save ()) {
// 				return $this->redirect ( [ 
// 						'dorm' 
// 				] );
// 			} else {
// 				return $this->render ( 'createDorm', [ 
// 						'model' => $model,
// 						'id' => $id 
// 				] );
// 			}
// 		} else
// 			return $this->redirect ( 'login' );
// 	}
	public function actionUpdatedorm() // 更新住宿信息
{
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			$model = DorminfoModel::find ()->where ( [ 
					'stu_id' => $id 
			] )->one ();
			
			if ($model->load ( \Yii::$app->request->post () ) && $model->save ()) {
				return $this->redirect ( [ 
						'dorm' 
				] );
			} else {
				return $this->render ( 'createDorm', [ 
						'model' => $model,
						'id' => $id 
				] );
			}
		} else
			return $this->redirect ( 'login' );
	}
	public function actionXjxx() {
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			$model = SchoolinfoModel::find ()->where ( [ 
					'stu_id' => $id 
			] )->one ();
			
			if ($model == null)
				return $this->render ( 'NotFound' );
			return $this->render ( 'xjxx', [ 
					'model' => $model,
					'id' => $id 
			] );
		} else
			return $this->redirect ( 'login' );
	}
	public function actionFamily() {
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			return $this->render ( 'family', [ 
					'id' => $id 
			] );
		} else
			return $this->redirect ( 'login' );
	}
	public function actionLiterary() {
		if (Check::check ()) {
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			return $this->render ( 'literary', [ 
					'id' => $id 
			] );
		} else {
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
		}
	}
	/**
	 * 学生界面：账户信息显示
	 * 密码修改
	 *
	 * @return string|\yii\web\Response
	 */
	public function actionInformation() {
		if (Check::check ()) {
			// 获取学生学号
			$id = \Yii::$app->session->get ( 'stu' ) ['stuid'];
			
			// 获取学生账号信息
			$infomodel = SchoolinfoModel::find ()->where ( [ 
					'stu_id' => $id 
			] )->one ();
			
			// 根据学号获取登录密码
			$model = Personinfo::find ()->where ( [ 
					'stu_id' => $id 
			] )->one ();
			
			// 获取 学生界面 -- 修改密码表单
			$pwdmodel = new PasswordAlertForm ();
			
			if ($pwdmodel->load ( \Yii::$app->request->post () )) {
				/**
				 * 验证用户输入的原密码是否正确
				 */
				if (\Yii::$app->getSecurity ()->validatePassword ($pwdmodel->pwd, $model->password_hash)){
					/**
					 * 原密码正确，则进行密码修改操作
					 */
					$model->password_hash = \Yii::$app->getSecurity ()->generatePasswordHash($pwdmodel->newpwd );
					/**
					 * 将修改后的密码保存到数据库中
					 */
					if ($model->save ()) {
						return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/information' );
					} else {
						return $this->render ( 'error', [ 
								'message' => '密码保存失败' 
						] );
					}
				} else {
					return $this->render ( 'error', [ 
							'message' => '原密码输入有误，请确认后重新输入' 
					] );
				}
			} else {
				return $this->render ( 'information', [ 
						'infomodel' => $infomodel,
						'pwdmodel' => $pwdmodel 
				] );
			}
		} else
			return $this->redirect ( \Yii::getAlias ( '@web' ) . '/front/login' );
	}

	public function actionStation() {
		return $this->render ( 'station' );
	}
	public function actionForgetpwdinfo() {
		return $this->render ( 'forgetpwdinfo' );
	}
	public function actionDormview($id) {
		return $this->render ( 'dormview', [ 
				'model' => $this->findModel ( $id ) 
		] );
	}


	public function actionDormupdate($id) {
		if (Check::check ()) {
			$model = $this->findModel ( $id );
			
			if ($model->load ( \Yii::$app->request->post () ) && $model->save ()) {
				return $this->redirect ( [ 
						'dormview',
						'id' => $model->id 
				] );
			} else {
				return $this->render ( 'dormupdate', [ 
						'model' => $model 
				] );
			}
		} else
			return $this->redirect ( "/login" );
	}
	protected function findModel($id) {
		if (($model = DorminfoModel::findOne ( $id )) !== null) {
			return $model;
		} else {
			throw new NotFoundHttpException ( 'The requested page does not exist.' );
		}
	}
}
